<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2018 http://www.hisiphp.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------

namespace app\example\admin;
use app\admin\controller\Admin;
use app\example\model\CollectArticle as NewsModel;
use app\example\model\CollectCategory as CategoryModel;
use think\Db;

/**
 * 征文
 * @package app\example\controller
 */
class Index extends Admin
{
    public function index()
    {
        $sort = input('param.sort/d',0);
        $_page = input('param.page/d', 0);
        $_keywords = input('param.keywords/s');
        $map = ['status' => 1];
        if ($_keywords) {
            $map['title'] = ['like', '%'.$_keywords.'%'];
        }
        $orderby = "ctime desc";
        if($sort == 1){
            $orderby = "vote_num asc";
        }else if($sort == 2){
            $orderby = "vote_num desc";
        }
        $data_list = Db::name('collect_article')->where($map)->order($orderby)->paginate(10, false, ['query' => input('get.')]);
        $pages = $data_list->render();
	if($sort == 0){
	    $sort = 2;
	}
        $this->assign("sort", $sort);
        $this->assign("pages", $pages);
        $this->assign('data_list', $data_list);
        return $this->fetch();
    }

    //统计
    public function statistical(){
        $sort = input('param.sort/d',0);
        $_page = input('param.page/d', 0);
        $_keywords = input('param.keywords/s');
        $map = " status=1 ";
        if ($_keywords) {
            $map .= " and title like '%$_keywords%'";
        }
        $orderby = "ctime desc";
        if($sort == 1){
            $orderby = "vote_num asc";
        }else if($sort == 2){
            $orderby = "vote_num desc";
        }
        $data_list = Db::name('collect_article')->where($map)->order($orderby)->paginate(10, false, ['query' => input('get.')]);
        $pages = $data_list->render();
	if($sort == 0){
	    $sort = 2;
	}
        $this->assign("sort", $sort);
        $this->assign("pages", $pages);
        $this->assign('data_list', $data_list);
        return $this->fetch();
    }

    //统计列表
    public function getvote(){
        $id = input('param.id/d',0);
        $map = "a.aid = $id";
        $data_list = Db::name('collect_vote as a')
                    ->join('wd_collect_user b ','a.uid = b.id','left')
                    ->field('b.teltphone,a.uid,a.vote_datetime')
                    ->where($map)
                    ->paginate(10, false, ['query' => input('get.')]);
        $pages = $data_list->render();
        $this->assign("pages", $pages);
        $this->assign('data_list', $data_list);
        return $this->fetch();
    }

    /**
     * 添加内容
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $mod = new NewsModel();
            if (!$mod->storage()) {
                return $this->error($mod->getError());
            }
            return $this->success('保存成功','index');
        }
        $category = CategoryModel::getSelect(CategoryModel::getChilds());
        $this->assign('category', $category);
        return $this->fetch('form');
    }

    /**
     * 修改内容
     * @return mixed
     */
    public function edit()
    {
        $id = get_num();
        if ($this->request->isPost()) {
            $mod = new NewsModel();
            if (!$mod->storage()) {
                return $this->error($mod->getError());
            }
            return $this->success('保存成功','index');
        }
        $row = NewsModel::where('id', $id)->find();
        if (!$row) {
            return $this->error('数据不存在');
        }
        $category = CategoryModel::getSelect(CategoryModel::getChilds());
        $this->assign('category', $category);
        $this->assign('data_info', $row);
        return $this->fetch('form');
    }
}
